﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.IO;
using System.Text;

namespace ChoViet.Lib.Helpers
{
    public static class OptionGroupListExtension
    {
        public static string OptionGroupList(this HtmlHelper helper, string idName, IEnumerable optionGroupData, string optionGroupDisplayValue, string optionProperty, string dataTextField, string dataValueField, string firstOption, string selectedValue, IDictionary<string, object> htmlAttributes, bool multiple)
        {
            StringBuilder htmlBuilder = new StringBuilder();

            using (TextWriter stringWriter = new StringWriter(htmlBuilder))
            {

                using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Id, idName);  
                    writer.AddAttribute(HtmlTextWriterAttribute.Name, idName);
                    if (htmlAttributes != null)
                    {
                        foreach (var k in htmlAttributes.Keys)
                        {
                            writer.AddAttribute(k.ToString(), htmlAttributes[k].ToString());                            
                        }
                    }
                    if (multiple)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Multiple, "multiple");
                    }
                    writer.RenderBeginTag(HtmlTextWriterTag.Select);

                    if (firstOption != null)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
                        writer.RenderBeginTag(HtmlTextWriterTag.Option);
                        writer.Write(firstOption);
                        writer.RenderEndTag();
                    }

                    foreach (object optionGroupDataItem in optionGroupData)
                    {
                        Type type = optionGroupDataItem.GetType();
                        PropertyInfo pinfo1 = type.GetProperty(optionGroupDisplayValue);
                        PropertyInfo pinfo2 = type.GetProperty(optionProperty);
                        Type type2 = pinfo2.GetType();
                        IEnumerable optionData = pinfo2.GetValue(optionGroupDataItem, null) as IEnumerable;
                        string val = pinfo1.GetValue(optionGroupDataItem, null) as string;
                        writer.WriteBeginTag("optgroup");
                        writer.WriteAttribute("label", val);
                        writer.Write(HtmlTextWriter.TagRightChar);

                        foreach (object optionDataItem in optionData)
                        {
                            PropertyInfo pinfo3 = optionDataItem.GetType().GetProperty(dataTextField);
                            string text = pinfo3.GetValue(optionDataItem, null) as string;
                            PropertyInfo pinfo4 = optionDataItem.GetType().GetProperty(dataValueField);
                            string value = Convert.ToString(pinfo4.GetValue(optionDataItem, null));
                            writer.AddAttribute(HtmlTextWriterAttribute.Value, value);
                            if (value == selectedValue)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
                            }
                            writer.RenderBeginTag(HtmlTextWriterTag.Option);
                            writer.Write(text);
                            writer.RenderEndTag();

                        }
                        writer.WriteEndTag("optgroup");
                    }
                    writer.RenderEndTag();
                }
            }
            return htmlBuilder.ToString(); ;
        }
        public static string OptionGroupList(this HtmlHelper helper, string idName, IEnumerable hierarchicalList, string optionGroupDisplayValue, string optionProperty, string optionDataText, string optionDataValue, string firstOption, string selectedValue)
        {
            return helper.OptionGroupList(idName, hierarchicalList, optionGroupDisplayValue, optionProperty, optionDataText, optionDataValue, firstOption,selectedValue,null,false);
        }
        public static string OptionGroupList(this HtmlHelper helper, string idName, IEnumerable hierarchicalList, string optionGroupDisplayValue, string optionProperty, string optionDataText, string optionDataValue, string firstOption, string selectedValue, IDictionary<string, object> htmlAttributes)
        {
            return helper.OptionGroupList(idName, hierarchicalList, optionGroupDisplayValue, optionProperty, optionDataText, optionDataValue, firstOption, selectedValue, htmlAttributes, false);
        }
        public static string OptionGroupList(this HtmlHelper helper, string idName, IEnumerable hierarchicalList, string optionGroupDisplayValue, string optionProperty, string optionDataText, string optionDataValue, string firstOption, string selectedValue, bool multiple)
        {
            return helper.OptionGroupList(idName, hierarchicalList, optionGroupDisplayValue, optionProperty, optionDataText, optionDataValue, firstOption, selectedValue,null, multiple);
        }
    }
}
